<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/19
 * Time: 16:56
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class SettingModel
{
    const TABLE = 'setting';

    /**
     * @param $code
     * @param $projectId
     * @return int|mixed
     */
    public function get($code, $projectId)
    {
        $info = $this->info(['code'=>$code,'projectId'=>$projectId]);
        if (empty($info)){
            return 0;
        }else{
            return $info['value'];
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data){
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    public function set($code, $value, $projectId)
    {
        DB::table(self::TABLE)->where(['code'=>$code,'projectId'=>$projectId,])->delete();
        return DB::table(self::TABLE)->insert(['code'=>$code,'name'=>$code,'value'=>$value,'projectId'=>$projectId,]);
    }
}